home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / dorskel2.arc / SAMPLE.C < prev    next >
C/C++ Source or Header  |  1991-12-05  |  6KB  |  183 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*  This Program Written by Paul Edwards.                            */
  4. /*                                                                   */
  5. /*  (Modified very slightly to work as a door by M. Kimes)           */
  6. /*  This module would replace DOORSKL4.C to create a working door.   */
  7. /*                                                                   */
  8. /*  The following block of notes are Paul's.                         */
  9. /*                                                                   */
  10. /*********************************************************************/
  11. /*********************************************************************/
  12. /*                                                                   */
  13. /*  Calendar - produce a calendar for any given year.                */
  14. /*                                                                   */
  15. /*  This program takes as a command line parameter a single number   */
  16. /*  which should be a 4 digit year.  It then prints out a calendar   */
  17. /*  corresponding to that year.  This program was inspired by a      */
  18. /*  shareware program which performed a similar function but didn't  */
  19. /*  come with source!                                                */
  20. /*                                                                   */
  21. /*  Many many thanks to Paul Schlyter, Stockholm, Sweden for his     */
  22. /*  absolutely fantastically brilliant dow macro.  However, due to   */
  23. /*  limitations of this macro (which are affected by anomolies of    */
  24. /*  the '/' and '%' operators on negative numbers) this program will */
  25. /*  generate invalid calendars for years such as 4 AD.  But I reckon */
  26. /*  anyone who wants to print out the gregorian calendar for 4 AD is */
  27. /*  a total wanker, especially when you consider the fact that the   */
  28. /*  gregorian calendar only came into existence in 1582.  There is   */
  29. /*  no upper limit to the year the calendar can print.  The program  */
  30. /*  also knows all the rules, ie the 4, 100, 400 year rules for a    */
  31. /*  leapyear, so that even if you don't know that 1900 wasn't a      */
  32. /*  leapyear, yet 2000 will be, this program knows!  Oh yeah, I'm    */
  33. /*  not sure exactly where the lower bound on calendars is, but it   */
  34. /*  will certainly work for anything above 1582.  Have fun!  Paul.   */
  35. /*                                                                   */
  36. /*  This program is dedicated to the public domain.                  */
  37. /*                                                                   */
  38. /*  Written 1990/8/28.                                               */
  39. /*                                                                   */
  40. /*********************************************************************/
  41.  
  42. #include "doorskel.h"
  43.  
  44.  
  45.  
  46. static int  _fastcall dayno(int yyyy, int mm, int x, int y);
  47. static void _fastcall prt3mon(int yr, int a, char *prtmon);
  48. int         _fastcall print_amonth (int yr, int month);
  49.  
  50.  
  51.  
  52.  
  53. void _fastcall mainloop (void) {
  54.  
  55.   int yr, wrkyr, dig1, dig2, dig3, dig4;
  56.   char yrstr[32];
  57.  
  58.  
  59.  
  60.   for(;;) {
  61.  
  62.     printm("\r\nYear for calendar? ([Enter] to quit)  ");
  63.     strcpy(yrstr,genin(5,0,0,0,NUM));
  64.     printm("\r\n");
  65.     if(!*yrstr) break;
  66.  
  67.     yr = atoi(yrstr);
  68.  
  69.     wrkyr = yr;
  70.     dig1 = wrkyr/1000;  wrkyr%=1000;
  71.     dig2 = wrkyr/100;  wrkyr%=100;
  72.     dig3 = wrkyr/10;  wrkyr%=10;
  73.     dig4 = wrkyr;
  74.     printfm("\r\n                                %d %d %d %d\r\n\r\n",
  75.             dig1,dig2,dig3,dig4);
  76.     prt3mon(yr, 0, "       JANUARY                 FEBRUARY"
  77.             "                   MARCH");
  78.     prt3mon(yr, 1, "        APRIL                     MAY"
  79.             "                     JUNE");
  80.     prt3mon(yr, 2, "        JULY                    AUGUST"
  81.             "                  SEPTEMBER");
  82.     prt3mon(yr, 3, "       OCTOBER                 NOVEMBER"
  83.             "                 DECEMBER");
  84.   }
  85. }
  86.  
  87.  
  88.  
  89.  
  90. static void _fastcall prt3mon (int yr, int a, char *prtmon) {
  91.  
  92.   /* print 3 months */
  93.  
  94.   static char *letters = " s  m  t  w  t  f  s     "
  95.                          " s  m  t  w  t  f  s     "
  96.                          " s  m  t  w  t  f  s\r\n";
  97.   int b, i, j, x;
  98.  
  99.  
  100.   printfm("%s\r\n\r\n",prtmon);
  101.   printfm("%s",letters);
  102.   for (i=0;i<6;i++) {
  103.     for (b=1;b<=3;b++) {
  104.       for (j=0;j<7;j++) {
  105.         x = dayno(yr,a*3+b,i,j);
  106.         if (x) printfm("%2d ",x);
  107.         else printfm("   ");
  108.       }
  109.       printfm("    ");
  110.     }
  111.     printfm("\r\n");
  112.   }
  113.   return;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. int _fastcall print_amonth (int yr, int month) {
  120.  
  121.     /* print an individual month...not used in this module... */
  122.  
  123.   int wrkyr, dig1, dig2, dig3, dig4;
  124.   char *mons[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG",
  125.                 "SEP","OCT","NOV","DEC"};
  126.   char *letters = " s  m  t  w  t  f  s\r\n";
  127.   int i, j, x;
  128.  
  129.  
  130.   wrkyr = yr;
  131.   dig1 = wrkyr/1000;  wrkyr%=1000;
  132.   dig2 = wrkyr/100;  wrkyr%=100;
  133.   dig3 = wrkyr/10;  wrkyr%=10;
  134.   dig4 = wrkyr;
  135.   printfm("\r\n     %d %d %d %d\r\n\r\n",
  136.       dig1,dig2,dig3,dig4);
  137.  
  138.   printfm("       %s\r\n",mons[month-1]);
  139.   printfm("%s",letters);
  140.   for (i=0;i<6;i++) {
  141.       for (j=0;j<7;j++) {
  142.         x = dayno(yr,month,i,j);
  143.         if (x) printfm("%2d ",x);
  144.         else printfm("   ");
  145.       }
  146.       printfm("\r\n");
  147.   }
  148.   printfm("\r\n");
  149.   return 0;
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. #define isleap(year) ((((year%4)==0) && ((year%100)!=0)) || \
  157.     ((year%400)==0))
  158.  
  159.  
  160.  
  161. #define dow(y,m,d)  \
  162. ( ( ( 3*(y) - (7*((y)+((m)+9)/12))/4 + (23*(m))/9 + (d) + 2 \
  163.     + (((y)-((m)<3))/100+1) * 3 / 4 - 15 ) % 7 ) )
  164.  
  165.  
  166.  
  167.  
  168. static int _fastcall dayno (int yyyy, int mm, int x, int y) {
  169.  
  170.   static int daytab[] = { 31, 28, 31, 30, 31, 30,
  171.                           31, 31, 30, 31, 30, 31};
  172.   int a, b, c;
  173.  
  174.  
  175.   a = dow(yyyy,mm,1);
  176.   b = x * 7 + y;
  177.   c = daytab[mm-1];
  178.   if ((mm == 2) && isleap(yyyy)) c++;
  179.   if (b < a) return (0);
  180.   if ((b-a) >= c) return (0);
  181.   return ((b-a+1));
  182. }
  183.